package cn.tedu.flower.manage.content.controller;

import cn.tedu.flower.manage.common.web.JsonResult;
import cn.tedu.flower.manage.content.pojo.param.AftermarketAddNewParam;
import cn.tedu.flower.manage.content.pojo.param.AftermarketUpdateInfoParam;
import cn.tedu.flower.manage.content.pojo.vo.AftermarketListItemVO;
import cn.tedu.flower.manage.content.pojo.vo.AftermarketStandardVO;
import cn.tedu.flower.manage.content.service.IAftermarketService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 处理售后相关请求的控制器类
 *
 * @author java@tedu.cn
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("content/aftermarket")
@Validated
@Api(tags = "2.7. 商品管理-售后管理")
public class AftermarketController {

    @Autowired
    private IAftermarketService aftermarketService;

    public AftermarketController() {
        log.debug("创建控制器对象：AftermarketController");
    }

    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/content/aftermarket/add-new')")
    @ApiOperation("添加售后")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid AftermarketAddNewParam aftermarketAddNewParam) {
        log.debug("开始处理【添加售后】的请求，参数：{}", aftermarketAddNewParam);
        aftermarketService.addNew(aftermarketAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/content/aftermarket/delete')")
    @ApiOperation("根据ID删除售后")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult delete(@PathVariable @Range(min = 1, message = "请提交有效的售后ID值！") Long id) {
        log.debug("开始处理【根据ID删除售后】的请求，参数：{}", id);
        aftermarketService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/content/aftermarket/update')")
    @ApiOperation("修改售后详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的售后ID值！") Long id,
                                     @Valid AftermarketUpdateInfoParam aftermarketUpdateInfoParam) {
        log.debug("开始处理【修改售后详情】的请求，ID：{}，新数据：{}", id, aftermarketUpdateInfoParam);
        aftermarketService.updateInfoById(id, aftermarketUpdateInfoParam);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/content/aftermarket/read')")
    @ApiOperation("根据ID查询售后详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "售后ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的售后ID值！") Long id) {
        log.debug("开始处理【根据ID查询售后详情】的请求，参数：{}", id);
        AftermarketStandardVO queryResult = aftermarketService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("/list")
    @PreAuthorize("hasAuthority('/content/aftermarket/read')")
    @ApiOperation("查询售后表")
    @ApiOperationSupport(order = 420)
    public JsonResult list() {
        log.debug("开始处理【获取类别树】的业务，参数：无");
        List<AftermarketListItemVO> aftermarketList = aftermarketService.list();
        return JsonResult.ok(aftermarketList);
    }
}
